import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/video/video_full_page.dart';
import 'package:tiktokadult/hj_page/video/view/video_menu_view.dart';
import 'package:tiktokadult/hj_utils/screen.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:video_player/video_player.dart';

import '../../tools_base/widget/net_image_widget.dart';

class VideoPlayerAlert extends StatefulWidget {
  final String? movieUrl;
  final String? imgCover;

  VideoPlayerAlert(this.movieUrl, {this.imgCover, Key? key}) : super(key: key);

  @override
  State<VideoPlayerAlert> createState() => _VideoPlayerAlertState();
}

class _VideoPlayerAlertState extends State<VideoPlayerAlert> {
  VideoPlayerController? _vpC;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _initStatus();
    });
  }

  void _initStatus() async {
    _vpC = VideoPlayerController.networkUrl(Uri.parse(widget.movieUrl ?? ""));
    await _vpC?.initialize();
    if (mounted) {
      _vpC?.play();
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      insetPadding: EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      child: SafeArea(
        child: Stack(
          fit: StackFit.expand,
          children: [
            _buildContent(),
            Positioned(
              left: 0,
              top: 0,
              child: InkWell(
                onTap: () => Navigator.of(context).pop(),
                child: Container(
                  padding: EdgeInsets.only(left: 16, top: 6),
                  child: Image.asset(
                    'back_circle.png'.commonImgPath,
                    width: 24,
                    height: 24,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildContent() {
    if (_vpC?.value.isInitialized != true) {
      return Stack(
        alignment: Alignment.center,
        children: [
          if (widget.imgCover?.isNotEmpty == true) NetworkImageLoader(imageUrl: widget.imgCover ?? ""),
          LoadingCenterWidget(),
        ],
      );
    } else {
      return Stack(
        fit: StackFit.expand,
        children: [
          Container(
            alignment: Alignment.center,
            width: screen.screenWidth,
            child: AspectRatio(
              aspectRatio: _vpC!.value.aspectRatio,
              child: VideoPlayer(_vpC!),
            ),
          ),
          VideoMenuView(
            controller: _vpC!,
            isFull: true,
            callback: () async {
              Get.to(VideoFullPage(controller: _vpC!, isAutoV: false));
            },
          ),
        ],
      );
    }
  }

  @override
  void dispose() {
    _vpC?.dispose();
    super.dispose();
  }
}
